/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.firebird.base.domain;

import java.io.Serializable;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qiuxj
 * @date 2009-11-14
 */
public class KeyLabel implements Comparable, Serializable {

	  private static final long serialVersionUID = 3689355407466181430L;
	  public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
	    public int compare(Object o1, Object o2) {
	      String label1 = ((KeyLabel)o1).getLabel();
	      String label2 = ((KeyLabel)o2).getLabel();
	      return label1.compareToIgnoreCase(label2);
	    }
	  };

	  private String label = null;

	  private String key = null;

	  public static Map converListToMap(List<KeyLabel> list)
	  {
	    Map map = new LinkedHashMap();

	    for (KeyLabel k : list) {
	      map.put(k.getKey(), k.getLabel());
	    }

	    return map;
	  }

	  public KeyLabel()
	  {
	  }

	  public KeyLabel(String key, String label)
	  {
	    this.label = label;
	    this.key = key;
	  }

	  public String getLabel()
	  {
	    return this.label;
	  }

	  public void setLabel(String label) {
	    this.label = label;
	  }

	  public String getKey()
	  {
	    return this.key;
	  }

	  public void setKey(String value) {
	    this.key = value;
	  }

	  public int compareTo(Object o)
	  {
	    String otherLabel = ((KeyLabel)o).getLabel();

	    return getLabel().compareTo(otherLabel);
	  }

	  public String toString()
	  {
	    StringBuffer sb = new StringBuffer("KeyLabel[");
	    sb.append(this.label);
	    sb.append(", ");
	    sb.append(this.key);
	    sb.append("]");
	    return sb.toString();
	  }

	  public boolean equals(Object obj)
	  {
	    if (obj == this) {
	      return true;
	    }

	    if (!(obj instanceof KeyLabel)) {
	      return false;
	    }

	    KeyLabel bean = (KeyLabel)obj;
	    int nil = (getKey() == null) ? 1 : 0;
	    nil += ((bean.getKey() == null) ? 1 : 0);

	    if (nil == 2)
	      return true;
	    if (nil == 1) {
	      return false;
	    }
	    return getKey().equals(bean.getKey());
	  }

	  public int hashCode()
	  {
	    return ((getKey() == null) ? 17 : getKey().hashCode());
	  }
	
}
